<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
      <template>
        <basic-form
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @closeModal="closeModal"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import basicForm from '../common/basicForm'
import { getLicenseByPid } from '@/views/app/biz/regisLicense/regisLicense'
import { getInfoByPid, saveInfo } from './deliveryAddress'

const s_FormInfo = [
  // { label: '所属单位', name: 'orgPid', type: 'input', placeholder: '请输入所属单位' },
  // { label: '所属部门', name: 'departPid', type: 'input', placeholder: '请输入所属部门' },
  { label: '收货地址', name: 'addr', type: 'input', placeholder: '请输入收货地址' },
  // { label: '收货地址经度', name: 'lng', type: 'input', placeholder: '请输入收货地址经度' },
  // { label: '收货地址纬度', name: 'lat', type: 'input', placeholder: '请输入收货地址纬度' },
  { label: '联系人', name: 'contact', type: 'input', placeholder: '请输入联系人' },
  { label: '联系电话', name: 'contactTel', type: 'input', placeholder: '请输入联系电话' },
  { label: '标签', name: 'tag', type: 'input', placeholder: '请输入标签' },
  // { label: '收货地址', name: 'addr', type: 'deliveryAddressSelect', placeholder: '请选择收货地址' },
  { label: ['保存', '重置'], type: 'botton' }
]

const s_formDatas = {
  sysSeq: 0,
  pid: '',
  orgPid: null,
  departPid: null,
  addr: null,
  lng: null,
  lat: null,
  contact: null,
  contactTel: null,
  tag: null
}
const s_rules = {
  addr: [{ required: true, message: '收货地址必填', trigger: 'blur' }],
  contact: [{ required: true, message: '联系人必填', trigger: 'blur' }],
  contactTel: [{ required: true, message: '联系电话必填', trigger: 'blur'}]
}
export default {
  components: {
    basicModal,
    basicForm
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增地址',
        modalWidth: '1000px'
      },
      s_FormInfo: s_FormInfo,
      s_formDatas: s_formDatas,
      s_rules: s_rules
    }
  },
  computed: {},
  mounted() {},
  methods: {
    //自动生成FormData
    autoFormData() {
      let str = ''
      for (const item of s_FormInfo) {
        str += item.name + ': null,'
      }
      console.log(str)
    },
    // 新增按钮 点击了新增
    addModal() {
      this.autoFormData()
      console.log('点击了新增')
      this.s_formDatas = {
        sysSeq: 0,
        pid: '',
        orgPid: null,
        departPid: null,
        addr: null,
        lng: null,
        lat: null,
        contact: null,
        contactTel: null,
        tag: null
      }
      this.modal.modalVisible = true
      this.modal.modalTitle = '新增地址'
    },
    // 编辑按钮
    editModal(val) {
      console.log('点击了编辑')
      this.getSBypid(val)
      this.modal.modalVisible = true
      this.modal.modalTitle = '修改地址'
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败', duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      console.log('formName', name)
      console.log('val', val)
      // switch (name) {
      //   case 'formName':
      //     // console.log('aaaaaaaaaaaaaa',name)
      //     // const zhucez = this.getZhuCezxx(val)
      //     break
      // }
    },
    // 获取注册证详细信息
    async getZhuCezxx(pid) {
      const res = await getLicenseByPid(pid)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.s_formDatas = Object.assign(this.s_formDatas, { zhuCezhPid: res.data.pid })
        this.s_formDatas = Object.assign(this.s_formDatas, { zhuCezh: res.data.zhengZhaohm })
        this.s_formDatas = Object.assign(this.s_formDatas, { shengChanqymc: res.data.shengChancsmc })
        this.s_formDatas = Object.assign(this.s_formDatas, { pinMing: res.data.chanPinmc })
      }
    },
    //
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    }
  }
}
</script>

<style></style>
